<?php

namespace App\Http\Controllers\User;

use App\Http\Classes\User;
use App\Http\Classes\UserLesson;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LessonController extends Controller
{
    /*
     * 用户添加课程
     * POST
     * lesson  date  start  end
     * lesson 课程库的ID
     */
    public function addLesson(Request $request)
    {
        /*
         * type   string
         * 1 语境
         * 2 心境
         */
        try
        {
            $data = $request->all();
            if ( isset($data['lesson']) && isset($data['date']) && isset($data['start']) && isset($data['end']) && isset($data['type']) )
            {
                $userLesson = new UserLesson();
                $user = new User($_COOKIE['USER_ID']);
                if ( $userLesson->userLessonCount($_COOKIE['USER_ID']) >= $userLesson->getMaxCount()
                    && ! $user->isVip() )
                {
                    return $this->response(0, '添加失败', [], '所添加的课程数量已超过上限');
                }

                $dataArray = [];
                $dataArray['lesson_id'] = $data['lesson'];
                $dataArray['date'] = $data['date'];
                $dataArray['start'] = $data['start'];
                $dataArray['end'] = $data['end'];
                $dataArray['type'] = $data['type'];
                $dataArray['user_id'] = $_COOKIE['USER_ID'];


                if ( $userLesson->addLesson($dataArray) )
                {
//                    $word = new Word();
//                    $sentence = new Sentence();
//
//                    $words = $word->getWordsByLessonId($dataArray['lesson_id']);
//                    $sentences = $sentence->getSentencesByLessonId($dataArray['lesson_id']);
//
//                    $dataWords = [];
//                    for ( $i = 0; $i < count($words); $i++ )
//                    {
//
//                    }
                    return $this->response(1, '添加成功', []);
                }
                else
                {
                    return $this->response(0, '添加失败', [], $userLesson->getMessage());
                }
            }
            else
            {
                return $this->response(0, '缺省参数', []);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 用户修改课程信息
     * POST
     */
    public function updateLesson(Request $request)
    {
        try
        {
            $data = $request->all();
            if ( isset($data['lesson']) && isset($data['date']) && isset($data['start']) && isset($data['end']) )
            {
                $dataArray = [];
                $dataArray['date'] = $data['date'];
                $dataArray['start'] = $data['start'];
                $dataArray['end'] = $data['end'];
                $userLesson = new UserLesson($data['lesson']);

                if ( $userLesson->updateLesson($dataArray) )
                {
                    return $this->response(1, '修改成功', []);
                }
                else
                {
                    return $this->response(0, '修改失败', [], $userLesson->getMessage());
                }
            }
            else
            {
                return $this->response(0, '缺省参数', []);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 获取用户所有课程计划
     * GET
     */
    public function getUserAllLessons()
    {
        try
        {
            $userId = $_COOKIE['USER_ID'];
            $lesson = new UserLesson();
            $lessons = $lesson->getUserLessons($userId);
            if ($lessons)
            {
                return $this->response(1, '获取成功', $lessons);
            }
            else
            {
                return $this->response(0, '无匹配数据', [], $lesson->getMessage());
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 获取用户单个课程计划
     * GET
     * lesson
     */
    public function getUserLesson(Request $request)
    {
        try
        {
            $data = $request->all();
            if ( isset($data['lesson']) )
            {
                $lesson = new UserLesson($data['lesson']);

                $lessoninfo = $lesson->getThisLesson();

                if ( $lessoninfo )
                {
                    return $this->response(1, '获取成功', $lessoninfo);
                }
                else
                {
                    return $this->response(0, '无匹配数据', $lessoninfo, $lesson->getMessage());
                }
            }
            else
            {
                return $this->response(0, '缺省参数', []);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 用户删除自己的某一课程计划
     * POST
     * lesson(int)
     */
    public function delLesson(Request $request)
    {
        try
        {
            $data = $request->all();

            if ( isset($data['lesson']) )
            {
                $lesson = new UserLesson($data['lesson']);

                if ( $lesson->delLesson($_COOKIE['USER_ID']) )
                {
                    return $this->response(1, '删除成功', []);
                }
                else
                {
                    return $this->response(0, '删除失败', [], $lesson->getMessage());
                }
            }
            else
            {
                return $this->response(0, '缺省参数', []);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 获取用户某天的课程
     * GET
     * date（默认当天）
     */
    public function getUserLessonDaily(Request $request)
    {
        try
        {
            $date = $request->has('date') ? $request->get('date') : date('Y-m-d');

            $lesson = new UserLesson();

            $lessons = $lesson->getLessonByDate($_COOKIE['USER_ID'], $date);

            if ( $lessons )
            {
                return $this->response(1, '获取成功', $lessons);
            }
            else
            {
                return $this->response(0, '无匹配数据', [], $lesson->getMessage());
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    public function getUserLessonWeek(Request $request)
    {
        try
        {
            $date = $request->has('date') ? $request->get('date') : date('Y-m-d');

            $lesson = new UserLesson();

            $lessons = $lesson->getLessonByWeek($_COOKIE['USER_ID'], $date);

            if ( $lessons )
            {
                return $this->response(1, '获取成功', $lessons);
            }
            else
            {
                return $this->response(0, '无匹配数据', [], $lesson->getMessage());
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    public function getUserLessonMonth(Request $request)
    {
        try
        {
            $date = $request->has('date') ? $request->get('date') : date('Y-m');

            $lesson = new UserLesson(null, $_COOKIE['USER_ID']);

            $lessons = $lesson->getLessonByMonth($date);

            if ( $lessons )
            {
                return $this->response(1, '获取成功', $lessons);
            }
            else
            {
                return $this->response(0, '无匹配数据', [], $lesson->getMessage());
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }
}
